<?php
/* SVN FILE: $Id: inventory.php 191 2009-09-01 22:19:51Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Manage member inventory
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 191 $
* @lastrevision $Date: 2009-09-01 22:19:51 +0000 (Tue, 01 Sep 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-09-01 22:19:51 +0000 (Tue, 01 Sep 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/modules_admin/manage/inventory.php $
*/


if ( ! defined( 'IN_ACP' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'admin.php'.";
	exit();
}

class admin_ibmarket_manage_inventory extends ipsCommand
{
	/**
	 * Skin object
	 *
	 * @access	private
	 * @var		object			Skin templates
	 */
	private $html;
	
	/**
	 * The member data to the member being edited
	 *
	 * @access	private
	 * @var		array
	 */
	private $_member;
	
	/**
	 * Main class entry point
	 *
	 * @access	public
	 * @param	object		ipsRegistry reference
	 * @return	void		[Outputs to screen]
	 */
	public function doExecute( ipsRegistry $registry )
	{
		$this->registry->getClass('class_permissions')->checkPermissionAutoMsg( 'member_edit', 'members', 'members' );
		
		$this->request['member_id'] = intval($this->request['member_id']);
		
		$this->_member = $this->DB->buildAndFetch(array('select'=>'*','from'=>'members','where'=>"member_id=".$this->request['member_id']));
		if(!$this->_member)
		{
			$this->registry->output->showError(null);
		}
		
		$this->html = $this->registry->output->loadTemplate('cp_skin_manage');
		
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'admin_market', 'admin_manage' ) );
		
		$execute = array(
							'form' => 'memberInventoryForm',
							'save' => 'saveMemberInventory',
							'add' => 'addMemberInventory',
						);
		
		if(!isset($execute[$this->request['do']]))
		{
			$this->request['do'] = 'form';
		}
		
		$function = $execute[$this->request['do']];
		$this->$function();
	}
	
	private function memberInventoryForm()
	{
		$inventoryItems = marketRegistry::getInventoryItem($this->request['member_id']);
		$this->registry->output->html = $this->html->manageInventory( $inventoryItems );
		
		$this->registry->getClass('output')->html_main .= $this->registry->getClass('output')->global_template->global_frame_wrapper();
		$this->registry->getClass('output')->sendOutput();
	}
	
	private function saveMemberInventory()
	{
		ipsRegistry::getClass('adminFunctions')->checkSecurityKey();
		
		foreach(marketRegistry::getInventoryItem($this->request['member_id']) as $invItem)
		{
			if($this->request['delete'][$invItem['i_id']])
			{
				$this->DB->delete('market_inventory',"i_id=".$invItem['i_id']);
				continue;
			}
			if(	isset($this->request['price'][$invItem['i_id']]) &&
				isset($this->request['resell'][$invItem['i_id']]) &&
				($invItem['price_bought'] != $this->request['price'][$invItem['i_id']] ||
				$invItem['resell_price'] != $this->request['resell'][$invItem['i_id']]))
			{
				$this->DB->update('market_inventory',array(
															'price_bought' => numbers::parseInt($this->request['price'][$invItem['i_id']]),
															'resell_price' => numbers::parseInt($this->request['resell'][$invItem['i_id']]),
								),"i_id=".$invItem['i_id']);
			}
		}
		
		$this->lang->words['save_inventory_log'] = str_replace("<#NAME#>",$this->_member['members_display_name'],$this->lang->words['save_inventory_log']);
		$this->lang->words['save_inventory_log'] = str_replace("<#ID#>",$this->_member['member_id'],$this->lang->words['save_inventory_log']);
		ipsRegistry::getClass('adminFunctions')->saveAdminLog( $this->lang->words['save_inventory_log'] );
		
		$this->registry->output->global_message = sprintf($this->lang->words['save_inventory_message'],$this->_member['members_display_name']);
		$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=manage&amp;section=inventory&amp;member_id=".$this->request['member_id'] );
	}
	
	private function addMemberInventory()
	{
		ipsRegistry::getClass('adminFunctions')->checkSecurityKey();
		
		$added = false;
		foreach($this->caches['market_items'] as $item)
		{
			if(	!isset($this->request['price'][$item['item_id']]) ||
				!isset($this->request['resell'][$item['item_id']]) ||
				!isset($this->request['quanity'][$item['item_id']]) ||
				$this->request['quanity'][$item['item_id']] == 0)
			{
				continue;
			}
			
			$expired = $item['expire'] ? IPS_UNIX_TIME_NOW + $item['expire'] : 0;
			
			for($i = 0; $i < $this->request['quanity'][$item['item_id']]; $i++)
			{
				$invItem = array(
									'member_id' => $this->request['member_id'],
									'item' => $item['item_id'],
									'price_bought' => numbers::parseInt($this->request['price'][$item['item_id']]),
									'resell_price' => numbers::parseInt($this->request['resell'][$item['item_id']]),
									'time_bought' => IPS_UNIX_TIME_NOW,
									'settings' => '',
									'expired' => $expired,
								);
				if(marketRegistry::getItem($item)->publicOnCreateItem($invItem))
				{
					$this->DB->insert('market_inventory',$invItem);
					$added = true;
				}
			}
		}
		
		if(!$added)
		{
			$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=manage&amp;section=inventory&amp;member_id=".$this->request['member_id'] );
		}
		
		$this->lang->words['add_inventory_log'] = str_replace("<#NAME#>",$this->_member['members_display_name'],$this->lang->words['add_inventory_log']);
		$this->lang->words['add_inventory_log'] = str_replace("<#ID#>",$this->_member['member_id'],$this->lang->words['add_inventory_log']);
		ipsRegistry::getClass('adminFunctions')->saveAdminLog( $this->lang->words['add_inventory_log'] );
		
		$this->registry->output->global_message = sprintf($this->lang->words['add_inventory_message'],$this->_member['members_display_name']);
		$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=manage&amp;section=inventory&amp;member_id=".$this->request['member_id'] );
	}
}